﻿Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.ComponentModel.Design.Serialization

Namespace System.Workflow.ComponentModel.Serialization
    Friend Class PrimitiveCodeDomSerializer
        Inherits CodeDomSerializer
        ' Methods
        Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, ByVal value As Object) As Object
            Dim expression As CodeExpression = New CodePrimitiveExpression(value)
            If ((((value Is Nothing) OrElse TypeOf value Is Boolean) OrElse (TypeOf value Is Char OrElse TypeOf value Is Integer)) OrElse (TypeOf value Is Single OrElse TypeOf value Is Double)) Then
                Dim service As CodeDomProvider = [TryCast](manager.GetService(GetType(CodeDomProvider)),CodeDomProvider)
                If ((Not service Is Nothing) AndAlso String.Equals(service.FileExtension, PrimitiveCodeDomSerializer.JSharpFileExtension)) Then
                    Dim context As ExpressionContext = [TryCast](manager.Context.Item(GetType(ExpressionContext)),ExpressionContext)
                    If ((Not context Is Nothing) AndAlso (context.ExpressionType Is GetType(Object))) Then
                        expression = New CodeCastExpression(value.GetType, expression)
                        expression.UserData.Add("CastIsBoxing", True)
                    End If
                End If
                Return expression
            End If
            If TypeOf value Is String Then
                Return expression
            End If
            Return New CodeCastExpression(New CodeTypeReference(value.GetType), expression)
        End Function


        ' Properties
        Friend Shared ReadOnly Property [Default] As PrimitiveCodeDomSerializer
            Get
                If (PrimitiveCodeDomSerializer.defaultSerializer Is Nothing) Then
                    PrimitiveCodeDomSerializer.defaultSerializer = New PrimitiveCodeDomSerializer
                End If
                Return PrimitiveCodeDomSerializer.defaultSerializer
            End Get
        End Property


        ' Fields
        Private Shared defaultSerializer As PrimitiveCodeDomSerializer
        Private Shared ReadOnly JSharpFileExtension As String = ".jsl"
    End Class
End Namespace

